package com.group.three.controller;


import com.github.pagehelper.PageInfo;
import com.group.three.pojo.VideoTag;
import com.group.three.pojo.vo.JsonResult;
import com.group.three.service.VideoTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/admin/tag")
public class VideoTagController {

    @Autowired
    private VideoTagService videoTagService;

    //=========================================日志操作tag页================================
    @RequestMapping("/goOpList")
    public ModelAndView goOpList(ModelAndView modelAndView) {
        modelAndView.setViewName("3-admin/oplog_list");
        return modelAndView;
    }

    @RequestMapping("/getOpList")
    @ResponseBody
    public JsonResult getOpList(Integer pageNum) {
        PageInfo<VideoTag> videoTagPageInfo = videoTagService.selectAllTag(pageNum, 10);
        return new JsonResult("200", "success", videoTagPageInfo);
    }

    @RequestMapping("/searchTagLogByUserNameOrTagName")
    @ResponseBody
    public JsonResult searchTagLog(String inputText) {

        List<VideoTag> videoTags = videoTagService.searchTagLogByUserNameOrTagName(inputText);
        return new JsonResult("200", "success", videoTags);
    }


    //==========================================标签添加=====================================
    @RequestMapping("/goAdd")
    public ModelAndView goAddTag(ModelAndView modelAndView) {
        modelAndView.setViewName("3-admin/tag_add");
        return modelAndView;
    }

    @RequestMapping("/add")
    @ResponseBody
    public JsonResult addTag(@RequestParam String inputData,
                             @RequestParam String userName, HttpServletRequest request) {
        String ip = request.getRemoteAddr();

        List<VideoTag> videoTags = videoTagService.searchTagLogByUserNameOrTagName(inputData);

        if (videoTags.size() != 0) {
            return new JsonResult("400", "已经存在此标签");
        }

        boolean addNewTag = videoTagService.addNewTag(inputData, userName, ip);
        if (addNewTag) {
            return new JsonResult("200", "success");
        } else {
            return new JsonResult("400", "error");
        }


    }


    //========================================标签管理controller=======================================
    @RequestMapping("/list")
    public ModelAndView getTagList(ModelAndView modelAndView) {

        PageInfo<VideoTag> videoTags = videoTagService.selectAllTag(1, 10);

        modelAndView.addObject("tagList", videoTags);
        modelAndView.setViewName("3-admin/tag_list");
        return modelAndView;
    }

    @RequestMapping("/changePage")
    @ResponseBody
    public JsonResult pageChange(Integer pageNum) {
        PageInfo<VideoTag> videoTags = videoTagService.selectAllTag(pageNum, 10);

        return new JsonResult("200", "success", videoTags);
    }

    /**
     * 通过id删除tag
     *
     * @param tagId tag的id
     * @return
     */
    @RequestMapping("/deleteTag")
    @ResponseBody
    public JsonResult deleteTag(Long tagId) {
        System.out.println(tagId);
        boolean b = videoTagService.deleteTagByTagId(tagId);
        if (b) {
            return new JsonResult("200", "success");
        } else {
            return new JsonResult("400", "success");
        }
    }

    @RequestMapping("/searchTag")
    @ResponseBody
    public JsonResult searchTag(String inputText) {
        List<VideoTag> searchByVideoTag = videoTagService.searchByVideoTag(inputText);
        return new JsonResult("200", "success", searchByVideoTag);
    }


}
